<template>
  <div class="app-container">
    <div class="add-talent-ducument-table">
      <el-table v-loading="loading" :data="documentList">
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">{{ (scope.$index + 1) }}</template>
        </el-table-column>
        <el-table-column label="证件类别" align="center" prop="documentType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.document_type" :value="scope.row.documentType" />
          </template>
        </el-table-column>
        <el-table-column label="证件注册类别" align="center" prop="registrationType" />
        <el-table-column label="证件附件" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="viewAttachments(scope.row.id)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="证件注册等级" align="center" prop="registrationLevel" />

        <el-table-column label="发证机关" align="center" prop="issuingAuthority" />
        <el-table-column label="证书编号" align="center" prop="certificateNumber"></el-table-column>
        <el-table-column label="发证日期" align="center" prop="issuingDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.issuingDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="注册有效期截止时间" align="center">
          <template slot-scope="scope">{{ '2099' }}</template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  getDocumentList,
  getAttachment,
  uploadAttachment
} from "@/api/system/document";

export default {
  props: {
    row: {
      type: Object,
      required: true // 必须传递该prop
    }
  },
  name: "Document",
  dicts: ["document_type"],
  data() {
    return {
      // 人才id
      talentId: null,
      // 用于控制文件上传按钮，是否显示上传/查看按钮
      isUploaded: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 证件信息表格数据
      documentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      url: "",
      // el-upload组件绑定的属性—— :file-list=“fileList”,渲染后fileList[index]是Object类型,而不是后台所需的File类型,
      // 而这个组件已经把对应的File类型存储到了fileList[index].raw这个属性里,直接拿来用就好.
      fileList: [],
      formData: "",
      attachments: [],
      dialogVisible: false
    };
  },
  created() {
    this.getList();
    console.log(this.dicts);
  },
  methods: {
    /** 查询证件信息列表 */
    getList() {
      this.loading = true;
      this.talentId = this.row.id;
      getDocumentList(this.talentId).then(response => {
        this.documentList = response.rows;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        attachs: [],
        talentId: null,
        documentName: null,
        documentType: null,
        registrationType: null,
        registrationLevel: null,
        issuingAuthority: null,
        certificateNumber: null,
        issuingDate: null,
        registrationExpirationDate: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        status: null,
        delFlag: null
      };
      this.resetForm("form");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    //自定义上传文件
    uploadFile(file) {
      // 创建FormData对象
      const formData = new FormData();
      // 添加文件到FormData对象中，'file'是后端接收文件参数的名称
      formData.append("file", file.file);
      uploadAttachment(formData).then(response => {
        this.form.attachs.push(response.msg);
      });
      console.log(formData);
    },
    // 文件列表变化
    handleChange(file, fileList) {
      this.fileList = fileList;
    },

    // 点击预览文件
    handlePreview(file) {
      console.log(file);
    },
    viewAttachments(id) {
      getAttachment(id)
        .then(response => {
          this.attachments = response.rows;
          this.dialogVisible = true;
        })
        .catch(error => {
          console.error("Error fetching attachments:", error);
        });
    }
  }
};
</script>

<style>
.add-talent-ducument-table {
  font: normal normal 8px "Helvetica Neue";
}
</style>